﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace Directory.Web
{
	public partial class MessageBoard : System.Web.UI.Page
	{
		private string pageKey = "page";
		protected int pageIndex;
		protected int totalRecords;
		protected int pageSize = WebSettings.GetInstance().MessageBoardPageSize;

		protected void Page_Load(object sender, EventArgs e)
		{
			if (!IsPostBack)
			{
				if (!int.TryParse(Request.QueryString[pageKey], out pageIndex))
				{
					pageIndex = 0;
				}

				Repeater1.DataSource = MessageBoardDataProxy.GetMessageList(true, pageIndex, pageSize, out totalRecords);
				Repeater1.DataBind();

				SetPageNav();
			}
		}

		private string GetPageNavigateUrl(int pageIndex)
		{
			string[] keys = HttpContext.Current.Request.QueryString.AllKeys;
			string query = null;
			foreach (string key in keys)
			{
				if (key == pageKey)
				{
					query += "&" + key + "=" + pageIndex.ToString();
				}
				else
				{
					query += "&" + key + Request.QueryString[key];
				}
			}
			if (query != null)
			{
				while (query.StartsWith("&"))
				{
					query = query.Substring(1);
				}
			}

			return HttpContext.Current.Request.Url.AbsolutePath + "?" + query;
		}

		private void SetPageNav()
		{
			int pageCounts = ((totalRecords - 1) / pageSize) + 1;
			lbCurrentPage.Text = (pageIndex + 1) + " / " + pageCounts;

			if (pageCounts < 1)
			{
				hplFirstPage.Enabled = false;
				hplPrePage.Enabled = false;
				hplNextPage.Enabled = false;
				hplLastPage.Enabled = false;
			}
			else if (pageIndex == 0)
			{
				hplFirstPage.Enabled = false;
				hplPrePage.Enabled = false;
				if (pageCounts == 1)
				{
					hplNextPage.Enabled = false;
					hplLastPage.Enabled = false;
				}
				else
				{
					hplNextPage.Enabled = true;
					hplNextPage.NavigateUrl = GetPageNavigateUrl(pageIndex + 1);
					hplLastPage.Enabled = true;
					hplLastPage.NavigateUrl = GetPageNavigateUrl(pageCounts);
				}
			}
			else if (pageIndex >= pageCounts - 1)
			{
				pageIndex = pageCounts - 1;
				hplFirstPage.Enabled = true;
				hplFirstPage.NavigateUrl = GetPageNavigateUrl(0);
				hplPrePage.Enabled = true;
				hplPrePage.NavigateUrl = GetPageNavigateUrl(pageIndex - 1);
				hplNextPage.Enabled = false;
				hplLastPage.Enabled = false;
			}
			else
			{
				hplFirstPage.Enabled = true;
				hplFirstPage.NavigateUrl = GetPageNavigateUrl(0);
				hplPrePage.Enabled = true;
				hplPrePage.NavigateUrl = GetPageNavigateUrl(pageIndex - 1);
				hplNextPage.Enabled = true;
				hplNextPage.NavigateUrl = GetPageNavigateUrl(pageIndex + 1);
				hplLastPage.Enabled = true;
				hplLastPage.NavigateUrl = GetPageNavigateUrl(pageCounts);
			}
		}

		protected void btnSubmit_Click(object sender, EventArgs e)
		{
			string author = string.Empty;
			string title = string.Empty;
			string content = string.Empty;
			try
			{
				content = txtContent.Text.Trim();
			}
			catch
			{
				ClientScript.RegisterStartupScript(this.GetType(), null, "<script>alert('内容不能为空')</script>");
				return;
			}
			try
			{
				title = txtTitle.Text.Trim();
			}
			catch
			{
				ClientScript.RegisterStartupScript(this.GetType(), null, "<script>alert('标题不能为空')</script>");
				return;
			}
			if (string.IsNullOrEmpty(title))
			{
				ClientScript.RegisterStartupScript(this.GetType(), null, "<script>alert('标题不能为空')</script>");
				return;
			}
			try
			{
				author = txtAuthor.Text.Trim();
			}
			catch
			{
			}

			try
			{
				MessageBoardDataProxy.CreateMessage(title, author, content, Request.UserHostAddress);
				txtAuthor.Text = "";
				txtContent.Text = "";
				txtTitle.Text = "";
				ClientScript.RegisterStartupScript(this.GetType(), null, "<script>alert('提交成功，通过审核后即可显示')</script>");
			}
			catch
			{
				ClientScript.RegisterStartupScript(this.GetType(), null, "<script>alert('提交失败：服务器错误。')</script>");
				return;
			}
		}
	}
}
